package br.com.ss.seguranca.model.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import br.com.ss.seguranca.model.entity.Perfil;
import br.com.ss.seguranca.model.entity.Rotina;
import br.com.ss.seguranca.model.entity.Sistema;

@Component
public class PerfilDAO {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void save(Perfil perfil) {
		entityManager.persist(perfil);
	}

	@Transactional
	public void merge(Perfil perfil) {
		entityManager.merge(perfil);
	}

	@Transactional
	public void remove(Perfil perfil) {
		Perfil entity = entityManager.merge(perfil);
		entityManager.remove(entity);
	}

	@SuppressWarnings("unchecked")
	public List<Perfil> list() {
		return entityManager.createQuery("select t from Perfil t")
				.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Perfil> listPorSistema(Sistema sistema) {
		Query query = entityManager
				.createQuery("select o from Perfil o " +
						"where o.sistema.id =:sistema_id " );
		query.setParameter("sistema_id", sistema.getId());
		List<Perfil> list = query.getResultList();
		return list;
	}

}
